-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
hlb = require('libhlb')
printf = require "mod_print"

-- -----------------------------------------------
local hzk16

function Read_dot_File(file_name)
	-- print("file_name: " .. file_name)
	local file = io.open(file_name, "r")

	local raw_data = {}
	local data = 0
	local line_num = 1

	for line in file:lines() do
		for i=1, line:len()/5 do
			 data = tonumber(line:sub(5 * i - 2 , 5 * i-1), 16)
			 if type(data) == "number" then
			 	raw_data[#raw_data + 1] = data
			 else
			 	print("Error to convert line: " .. line_num .. " of: " .. line:sub(5 * i - 2 , 5 * i-1) )
			 end
		end
		line_num = line_num + 1
	end

	file:close()

	return raw_data
end

function get_word_dot(addr)
    local l_dot = {}

    --print(string.format("addr + 32 = 0x%06x, hzk16:len()+1 = 0x%06x", addr + 32, hzk16:len()+1))
    if (addr + 32) <= (hzk16:len()+1) then
        for i=1, 32 do
            l_dot[i] = hzk16:byte(i+addr)
        end
    end
    
    return l_dot
end

function print_hz_h(word_dot)
    local mask

    for i=1, 16 do
        for j=1, 2 do
            mask = 0x80
            for k=1, 8 do
                if bit32.btest(word_dot[(i-1)*2 + j], mask) then
                    io.write("* ")
                else
                    io.write("  ")
                end
                mask = mask / 2
            end
        end
        print("")
    end
end

function print_hz_v(word_dot)
    local mask = 1

    for j=1, 8 do
        for i=1, 16 do
            if bit32.btest(word_dot[i], mask) then
                io.write("* ")
            else
                io.write("  ")
            end
        end
        print("")
        mask = mask * 2
    end
    mask = 1
    for j=1, 8 do
        for i=1, 16 do
            if bit32.btest(word_dot[i+16], mask) then
                io.write("* ")
            else
                io.write("  ")
            end
        end
        print("")
        mask = mask * 2
    end
end

function exchange(mat)
    local ret_mat = {}
    local mask0, mask1
    local index = 1

    if #mat ~= 32 then
        return
    end
    for i=1, 32 do
        ret_mat[i] = 0
    end

    for i=0, 1 do
        mask0 = 0x80
        for k=1, 8 do
            mask1 = 0x01
            for j=1,15, 2 do
                if bit32.btest(mat[i + j], mask0) then
                    ret_mat[index] = ret_mat[index] + mask1
                end
                --print("i+j = " .. i+j)
                mask1 = mask1 * 2
            end
            mask0 = mask0 / 2
            index = index + 1
        end
    end
    for i=0, 1 do
        mask0 = 0x80
        for k=1, 8 do
            mask1 = 0x01
            for j=1,15, 2 do
                if bit32.btest(mat[i + j + 16], mask0) then
                    ret_mat[index] = ret_mat[index] + mask1
                end
                --print("i+j = " .. i+j)
                mask1 = mask1 * 2
            end
            mask0 = mask0 / 2
            index = index + 1
        end
    end

    return ret_mat
end
-- ===================================
if false then
    local file = io.open("HZK16_H.bin", "rb")
    hzk16 = file:read("*all")
    file:close()

    local dot = get_word_dot(0xb040)
    printf.table(dot, true)
    print_hz_h(dot)
    dot = exchange(dot)
    printf.table(dot, true)
    print_hz_v(dot)
end

-- ===================================
-- start : 0x69C0
--  new  : 0xB040
if false then
    local file = io.open("HZK16_V.bin", "rb")
    hzk16 = file:read("*all")
    file:close()
    for j=0, 86 do
        local base = j*94*32
        print("=======================")
        print("Start address : " .. base .. ", At Local : " .. j )
        for i=0, 7 do
            local dot = get_word_dot(base+i*32)
            --printf.table(dot, true)
            print_hz_v(dot)
        end
    end
end
-- ===================================
if true then
    local file = io.open("HZK16_V.bin", "rb")
    hzk16 = file:read("*all")
    file:close()

    local file_w = io.open("hwatch_font.bin", "wb")
    file_w:write(hzk16)
    for i=hzk16:len(),0x3cf00-1 do
        file_w:write(string.char(0))
    end
    -- download dox 8x16
    local dot_8x16 = Read_dot_File("ascii_8x16_dot.txt")
    --printf.table(dot_8x16, true)

    for i=1,#dot_8x16 do
        file_w:write(string.char(dot_8x16[i]))
    end

    file_w:close()
end
-- ===================================
if false then
    local file = io.open("HZK16_H.bin", "rb")
    hzk16 = file:read("*all")
    file:close()

    local file_w = io.open("HZK16_V.bin", "wb")
    local dot
    for offset = 1, hzk16_h:len(), 32 do
        dot = get_word_dot(offset-1)
        dot = exchange(dot)
        for w=1, 32 do
            file_w:write(string.char(dot[w]))
        end
    end

    file_w:close()
end